import { Controller } from 'egg';


class SchoolController extends Controller {
    /**
     * 查询学校列表
     */
    public async searchSchoolList() {
        const { ctx } = this;
        const resultSchool = await this.ctx.service.schoolService.searchSchoolList();
        ctx.body = { code: 1, msg: '查询成功', data: resultSchool };
    }

    /**
     * 创建学校
     */
    public async createSchool() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                school_name: 'string',
                school_badge: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        let { school_badge, school_name, college } = request.body;
        if (college) {
            try {
                college = JSON.parse(college);
            } catch (e) {
                ctx.body = ctx.helper.responseErrorParam("学院参数格式错误");
                return;
            }
        }
        await this.ctx.service.schoolService.createSchool(school_name, school_badge, college);
        ctx.body = { code: 1, msg: '创建成功' };
    }

    /**
     * 启用、停用学校
     */
    public async startAndStopSchool() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                school_id: 'string',
                type: 'type'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const { school_id, type } = request.body;
        await this.ctx.service.schoolService.startAndStopSchool(school_id, type.toString());
        ctx.body = { code: 1, msg: '操作成功' };
    }

    /**
     * 根据id查找学校
     */
    public async searchSchoolById() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                school_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const { school_id } = request.body;
        const result = await this.ctx.service.schoolService.searchSchoolById(school_id);
        ctx.body = { code: 1, msg: '查询成功', data: result };
    }

    /**
     * 保存修改
     */
    public async updateSchool() {
        const { ctx } = this;
        try {
            ctx.validate({
                school_id: 'string',
                school_name: 'string',
                school_badge: 'string',
            }, ctx.request.body);
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const { school_id, school_name, school_badge } = ctx.request.body;
        await this.ctx.service.schoolService.updateSchool(school_id, school_name, school_badge);
        ctx.body = { code: 1, msg: '修改成功' };
    }

    /**
     * 添加学院
     */
    public async createCollege() {
        const { ctx } = this;
        try {
            ctx.validate({
                school_id: 'string',
                college_name: 'string',
                age_limit: 'int'
            }, ctx.request.body);
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const { college_name, school_id, age_limit } = ctx.request.body;
        await this.ctx.service.schoolService.createCollege(college_name, school_id, age_limit);
        ctx.body = { code: 1, msg: '添加成功' };
    }

    /**
     * 启用、停用学院
     */
    public async startAndStopCollege() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                college_id: 'string',
                type: 'type'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await this.ctx.service.schoolService.startAndStopCollege(request.body);
        ctx.body = ctx.helper.responseSuccess('操作成功');
    }

    /**
     * 修改学院
     */
    public async updateCollege() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                college_id: 'string',
                college_name: 'string',
                age_limit: 'int'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await this.ctx.service.schoolService.updateCollege(request.body);
        ctx.body = ctx.helper.responseSuccess('修改成功');
    }

    /**
     * 根据学校id获取学院列表
     */
    public async searchCollegeBySchoolId() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                school_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await this.ctx.service.schoolService.searchCollegeBySchoolId(request.body);
        ctx.body = ctx.helper.responseSuccess('获取成功', result);
    }

    /**
     * 根据学院名查找学院
     */
    public async searchCollegeByName() {
        const { ctx, ctx: { request } } = this;
        const result = await this.ctx.service.schoolService.searchCollegeByName(request.body);
        ctx.body = ctx.helper.responseSuccess('获取成功', result);
    }
}
export default SchoolController;